package com.lx.oms.common.utils.excel;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


/**
 * excel导出工具类
 * 
 * @author houzhong
 * 
 */
public class ExcelExportUtils {

	private static Map<Class<?>, ExcelDataConvert<?>> converts = new HashMap<Class<?>, ExcelDataConvert<?>>();

	static {
		converts.put(Date.class, new SimpleDateConvert());
	}

	/**
	 * 创建表头
	 * 
	 * @author lhh@gongren.com
	 * @since 2017年5月9日 下午5:24:00
	 * @param wb
	 * @param clazz
	 * @return
	 */
	private static HSSFSheet createTableHead(HSSFWorkbook wb, Class<?> clazz) {
		HSSFSheet sheet = wb.createSheet();
		HSSFRow row = sheet.createRow(0);
		Field[] fields = clazz.getDeclaredFields();
		ExportFieldAnnotation buffer = null;
		for (int i = 0, len = fields.length; i < len; i++) {
			Field field = fields[i];
			if (field != null && null != (buffer = field.getAnnotation(ExportFieldAnnotation.class))) {
				row.createCell(buffer.index()).setCellValue(buffer.title());
			}
		}
		return sheet;
	}

	private static <T> HSSFSheet segtTableValue(HSSFSheet sheet, List<T> exportList) throws Exception {
		if (exportList == null || exportList.size() == 0) {
			return sheet;
		}
		if (sheet == null) {
			throw new RuntimeException("sheet为 null");
		}
		Object first = null;
		int len = exportList.size();
		for (int i = 0; i < len; i++) {
			if ((first = exportList.get(i)) != null) {
				break;
			}
		}
		Field[] fields = first.getClass().getDeclaredFields();
		Object buffer = null;
		ExportFieldAnnotation annotationBuffer = null;
		for (int i = 0; i < len; i++) {
			buffer = exportList.get(i);
			HSSFRow row = sheet.createRow(i + 1);
			if (buffer == null) {
				continue;
			}
			I: for (int j = 0; j < fields.length; j++) {
				Field field = fields[j];
				if (field == null || (annotationBuffer = field.getAnnotation(ExportFieldAnnotation.class)) == null) {
					continue I;
				}
				String text = getText(buffer, field);
				row.createCell(annotationBuffer.index()).setCellValue(text);
			}
		}
		return sheet;
	}

	private static String getText(Object obj, Field field) throws Exception {
		field.setAccessible(true);
		Object val = field.get(obj);
		if (val == null) {
			return "";
		}
		if (converts == null || converts.size() == 0) {
			return val == null ? "" : val.toString();
		}
		Type type = field.getType();
		Class<?> c = Class.class.cast(type);
		Set<Class<?>> keySet = converts.keySet();
		for (Class<?> class1 : keySet) {
			if (class1.equals(c)) {
				ExcelDataConvert<?> dataConvert = converts.get(c);
				return dataConvert.convert(val);
			}
		}
		return val == null ? "" : val.toString();
	}

	/**
	 * 生成excel
	 * 
	 * @author lhh@gongren.com
	 * @since 2017年5月9日 下午5:30:59
	 * @param exportList
	 * @param clazz
	 * @return
	 * @throws Exception
	 */
	public static <T> HSSFWorkbook process(List<T> exportList, Class<T> clazz) throws Exception {
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = ExcelExportUtils.createTableHead(wb, clazz);
		ExcelExportUtils.segtTableValue(sheet, exportList);
		return wb;
	}

}
